<?php

class caiwuAction extends Action
{
    public function index()
    {
        $model = new Model();
        $name = I('post.sousuo');
        $where = "s.projectid=p.id and p.custid=c.id and p.employee=e.id and p.type=pt.id and t.site=s.id   and t.type=9";
        if (IS_POST || !empty($name))
        {
            $where = $where . " and c.company like '%{$name}%'";
        }

        $data = $model->table('ys_project p ,ys_employee e,ys_projecttype pt,ys_customer c,ys_site s,ys_task t')
            ->where($where)
            ->field('s.id as sid,c.*,pt.name as ptname,e.name as ename,s.keyword,s.domain,t.id as tid,t.note as con,s.beinumber as bn,s.status as sta')
            ->order('s.id desc')
            ->select();

        // echo $model->getLastSql();exit();
        $this->list = $data;
        $this->display();
    }

    public function beian()
    {
        $model = new Model();
        $id = I('post.sid');
        $where = "s.projectid=p.id and p.custid=c.id and p.employee=e.id and p.type=pt.id and t.site=s.id  and s.id=$id ";
        $data = $model->table('ys_project p ,ys_employee e,ys_projecttype pt,ys_customer c,ys_site s,ys_task t')
            ->where($where)
            ->field('s.id as sid,c.*,pt.name as ptname,e.name as ename,s.keyword,s.domain,t.id as tid')
            ->order('s.id')
            ->find();
        $this->assign('data', $data);
        $this->display();
    }

    public function beiannumber()
    {
        $model = new Model();
        $id = I('post.sid');
        $where = "s.projectid=p.id and p.custid=c.id and p.employee=e.id and p.type=pt.id and t.site=s.id  and s.id=$id ";
        $data = $model->table('ys_project p ,ys_employee e,ys_projecttype pt,ys_customer c,ys_site s,ys_task t')
            ->where($where)
            ->field('s.id as sid,c.*,pt.name as ptname,e.name as ename,s.keyword,s.domain,t.id as tid')
            ->order('s.id')
            ->find();
        $this->assign('data', $data);
        $this->display();
    }

    public function youdi()
    {
        $site = M('site');
        $data = array();
        $data['id'] = I('post.sid');
        $data['youditime'] = date('Y-m-d H:i:s');
        $data['status'] = 3;
        $data['yunnumber'] = I('post.yundan');
        $count = $site->save($data);

        //	echo $task->getLastSql();exit();
        if ($count > 0)
        {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("caiwu/index"));
            $this->success("邮寄完成");
        } else
        {
            $this->error("没有完成");
        }
    }

    public function wancheng()
    {
        $site = M('site');
        $data = array();
        $data['id'] = I('post.sid');
        $data['finishtime'] = date('Y-m-d H:i:s');
        $data['status'] = 4;
        $data['beinumber'] = I('post.beian');
        $count = $site->save($data);

        //	echo $task->getLastSql();exit();
        if ($count > 0)
        {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("caiwu/index"));
            $this->success("完成");
        } else
        {
            $this->error("没有完成");
        }
    }


    public function del()
    {
        $id = $_GET['id'];
        //echo($id);
        $User = M("Caiwu"); // 实例化User对象
        $User->where("id=$id")->delete(); // 删除id数据
        $this->show('<script type="text/javascript" src="__PUBLIC__/libs/js/jquery.js"></script><script type="text/javascript">$(function(){top.Dialog.alert("删除成功！",function(){window.parent.frmright.location.reload();top.Dialog.close()})})</script>', 'utf-8');
    }

    public function shouru()
    {
        $account = D('Caiwu'); // 实例化模型类
        $count = $account->where("paytype='收入'")->count();
        //分页显示文章列表，每页20篇文章
        import('ORG.Util.Page');
        $page = new Page($count, 20); //后台管理页面默认一页显示8条文章记录
        $page->setConfig('prev', "上一页"); //上一页
        $page->setConfig('next', '下一页'); //下一页
        $page->setConfig('first', '首页'); //第一页
        $page->setConfig('last', '尾页'); //最后一页
        $page->setConfig('theme', ' %first% %upPage%  %linkPage%  %downPage% %end%');
        //设置分页回调方法
        $show = $page->show();
        $firstpage = $page->firstRow;
        $lastpage = $page->listRows;
        $list = $account->query("select * from ys_caiwu where paytype='收入' order by id desc limit $firstpage,$lastpage");
        $shouruheji = $account->where("paytype='收入'")->sum('shishou');
        //echo($shouruheji);
        $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
        $this->assign('accounts_list', $list); // 数据列表变量赋值
        $this->assign('page_method', $show); //分页列表变量赋值

        $this->display(index);
    }

    public function addshouru()
    {
        $payitem = M('Payitem');
        $payitemlist = $payitem->query("select item from ys_payitem where paytype='收入' order by id asc");
        $this->assign('payitemlist', $payitemlist); // 数据列表变量赋值
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display(addshouru);
    }

    public function addsave()
    {
        $username = session('rname'); //获取session的用户名
        $newmark = D('Caiwu'); // 实例化模型类
        $data['addtime'] = date('Y-m-d H:i:s');
        $data['adduser'] = $username;
        $data['shishou'] = $_POST['shishou'];
        $data['yingshou'] = $_POST['yingshou'];
        if ($_POST['shishou'] > $_POST['yingshou'])
        {
            $data['qiankuan'] = "";
            $data['shifou'] = "no";
        } else
        {
            $data['qiankuan'] = $_POST['qiankuan'];
            $data['shifou'] = "yes";
        }
        $data['payway'] = $_POST['payway'];
        $data['payitem'] = $_POST['payitem'];
        $data['payuser'] = $_POST['payuser'];
        $data['contractdate'] = $_POST['contractdate'];
        $data['note'] = $_POST['note'];
        $data['paytype'] = '收入';
        $newmark->add($data);
        $this->show('<script type="text/javascript" src="__PUBLIC__/libs/js/jquery.js"></script><script type="text/javascript">$(function(){top.Dialog.alert("成功提交",function(){window.parent.frmright.location.reload();top.Dialog.close()})})</script>', 'utf-8');
    }

    public function editshouru()
    {
        $id = $_GET['id'];
        $editshouru = M('Caiwu');
        $data = $editshouru->where("id=$id")->find();
        //echo($editshouru->getLastSql());
        $this->assign('id', $id);
        $this->assign('shishou', $data['shishou']);
        $this->assign('yingshou', $data['yingshou']);
        $this->assign('qiankuan', $data['qiankuan']);
        $this->assign('payway', $data['payway']);
        $this->assign('payitem', $data['payitem']);
        $this->assign('payuser', $data['payuser']);
        $this->assign('contractdate', $data['contractdate']);
        $this->assign('note', $data['note']);
        $payitem = M('Payitem');
        $payitemlist = $payitem->query("select item from ys_payitem where paytype='收入' order by id asc");
        $this->assign('payitemlist', $payitemlist); // 数据列表变量赋值
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function shourueditsave()
    {
        $id = $_GET['id'];
        header("Content-Type:text/html; charset=utf-8");
        $custom = M('Caiwu');
        $yingshou = $_POST['yingshou'];
        $shishou = $_POST['shishou'];
        $qiankuan = $_POST['qiankuan'];
        $lasttime = date('Y-m-d H:i:s');
        if ($shishou > $yingshou or $qiankuan <= 0 or $qiankuan == "")
        {
            $qiankuan = "";
            $shifou = 'no';
        } else
        {
            $shishou = $_POST['shishou'];
            $shifou = 'yes';
        }
        $data = array('shishou' => $shishou, 'yingshou' => $yingshou, 'qiankuan' => $qiankuan, 'payway' => $_POST['payway'], 'payitem' => $_POST['payitem'], 'payuser' => $_POST['payuser'], 'contractdate' => $_POST['contractdate'], 'note' => $_POST['note'], 'lasttime' => $lasttime);
        $id = $_GET['id'];
        $custom->where('id=' . $id)->setField($data); // 根据条件保存修改的数据
        $this->show('<script type="text/javascript" src="__PUBLIC__/libs/js/jquery.js"></script><script type="text/javascript">$(function(){top.Dialog.alert("成功提交",function(){window.parent.frmright.location.reload();top.Dialog.close()})})</script>', 'utf-8');
    }

    public function qiankuan()
    {

        $account = D('Caiwu'); // 实例化模型类
        $count = $account->count();
        //分页显示文章列表，每页20篇文章
        import('ORG.Util.Page');
        $page = new Page($count, 20); //后台管理页面默认一页显示8条文章记录
        $page->setConfig('prev', "上一页"); //上一页
        $page->setConfig('next', '下一页'); //下一页
        $page->setConfig('first', '首页'); //第一页
        $page->setConfig('last', '尾页'); //最后一页
        $page->setConfig('theme', ' %first% %upPage%  %linkPage%  %downPage% %end%');
        //设置分页回调方法
        $show = $page->show();
        $firstpage = $page->firstRow;
        $lastpage = $page->listRows;
        $list = $account->query("select * from ys_caiwu where paytype='收入' and shifou='yes' order by id desc limit $firstpage,$lastpage");
        $this->assign('accounts_list', $list); // 数据列表变量赋值
        $this->assign('page_method', $show); //分页列表变量赋值
        $this->display();
    }

    public function zhichu()
    {
        $leixing = $this->_param(3); //获取GET的第三个参数，员工名称
        //echo($leixing);
        $account = D('Caiwu'); // 实例化模型类
        $count = $account->count();
        //分页显示文章列表，每页20篇文章
        import('ORG.Util.Page');
        $page = new Page($count, 20); //后台管理页面默认一页显示8条文章记录
        $page->setConfig('prev', "上一页"); //上一页
        $page->setConfig('next', '下一页'); //下一页
        $page->setConfig('first', '首页'); //第一页
        $page->setConfig('last', '尾页'); //最后一页
        $page->setConfig('theme', ' %first% %upPage%  %linkPage%  %downPage% %end%');
        //设置分页回调方法
        $show = $page->show();
        $firstpage = $page->firstRow;
        $lastpage = $page->listRows;
        if ($leixing == 'zuoji')
        {
            $list = $account->query("select * from ys_caiwu where paytype='支出'  and payitem='座机费用' order by id desc limit $firstpage,$lastpage");
            $shouruheji = $account->where("paytype='支出' and payitem='座机费用'")->sum('shishou');
            //echo($shouruheji);
            $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
        } else
        {
            if ($leixing == 'chailv')
            {
                $list = $account->query("select * from ys_caiwu where paytype='支出'  and payitem='差旅费用' order by id desc limit $firstpage,$lastpage");
                $shouruheji = $account->where("paytype='支出' and payitem='差旅费用'")->sum('shishou');
                //echo($shouruheji);
                $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
            } else
            {
                if ($leixing == 'bangong')
                {
                    $list = $account->query("select * from ys_caiwu where paytype='支出'  and payitem='办公费用' order by id desc limit $firstpage,$lastpage");
                    $shouruheji = $account->where("paytype='支出' and payitem='办公费用'")->sum('shishou');
                    //echo($shouruheji);
                    $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
                } else
                {
                    if ($leixing == 'zhaodai')
                    {
                        $list = $account->query("select * from ys_caiwu where paytype='支出'  and payitem='招待费用' order by id desc limit $firstpage,$lastpage");
                        $shouruheji = $account->where("paytype='支出' and payitem='招待费用'")->sum('shishou');
                        //echo($shouruheji);
                        $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
                    } else
                    {
                        if ($leixing == 'huodong')
                        {
                            $list = $account->query("select * from ys_caiwu where paytype='支出'  and payitem='活动费用' order by id desc limit $firstpage,$lastpage");
                            $shouruheji = $account->where("paytype='支出' and payitem='活动费用'")->sum('shishou');
                            //echo($shouruheji);
                            $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
                        } else
                        {
                            if ($leixing == 'peixun')
                            {
                                $list = $account->query("select * from ys_caiwu where paytype='支出'  and payitem='培训费用' order by id desc limit $firstpage,$lastpage");
                                $shouruheji = $account->where("paytype='支出' and payitem='培训费用' ")->sum('shishou');
                                //echo($shouruheji);
                                $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
                            } else
                            {
                                if ($leixing == 'guoqiao')
                                {
                                    $list = $account->query("select * from ys_caiwu where paytype='支出'  and payitem='过桥费用' order by id desc limit $firstpage,$lastpage");
                                    $shouruheji = $account->where("paytype='支出' and payitem='过桥费用'")->sum('shishou');
                                    //echo($shouruheji);
                                    $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
                                } else
                                {
                                    if ($leixing == 'jiayou')
                                    {
                                        $list = $account->query("select * from ys_caiwu where paytype='支出'  and payitem='加油费用' order by id desc limit $firstpage,$lastpage");
                                        $shouruheji = $account->where("paytype='支出' and payitem='加油费用'")->sum('shishou');
                                        //echo($shouruheji);
                                        $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
                                    } else
                                    {
                                        if ($leixing == 'gongzi')
                                        {
                                            $list = $account->query("select * from ys_caiwu where paytype='支出'  and payitem='工资费用' order by id desc limit $firstpage,$lastpage");
                                            $shouruheji = $account->where("paytype='支出' and payitem='工资费用'")->sum('shishou');
                                            //echo($shouruheji);
                                            $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
                                        } else
                                        {
                                            $list = $account->query("select * from ys_caiwu where paytype='支出' order by id desc limit $firstpage,$lastpage");
                                            $shouruheji = $account->where("paytype='支出'")->sum('shishou');
                                            //echo($shouruheji);
                                            $this->assign('shouruheji', $shouruheji); // 数据列表变量赋值
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $this->assign('accounts_list', $list); // 数据列表变量赋值
        $this->assign('page_method', $show); //分页列表变量赋值
        if (is_numeric($leixing) or empty($leixing))
        {
            $this->display();
        } else
        {
            $this->display($leixing);
        }

    }

    public function addzuoji()
    {
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function addchailv()
    {
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function addbangong()
    {
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function addpeixun()
    {
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function addzhaodai()
    {
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function addhuodong()
    {
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function addguoqiao()
    {
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function addjiayou()
    {
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function addgongzi()
    {
        $user = M('User');
        $userlist = $user->query("select rname from ys_user  order by id asc");
        $this->assign('userlist', $userlist); // 数据列表变量赋值
        $this->display();
    }

    public function zhichusave()
    {
        $username = session('rname');
        $newitem = M('Caiwu'); // 实例化模型类
        if ($newitem->create())
        {
            $newitem->addtime = date('Y-m-d H:i:s');
            $newitem->adduser = $username;
            $newitem->paytype = '支出';
            $vo = $newitem->add();
            //$newitem->getLastSql();
            $this->show('<script type="text/javascript" src="__PUBLIC__/libs/js/jquery.js"></script><script type="text/javascript">$(function(){top.Dialog.alert("成功提交",function(){window.parent.frmright.location.reload();top.Dialog.close()})})</script>', 'utf-8');
        } else
        {
            echo '信息添加失败，请检查网络！';
        }
    }


}